package com.lhy.controller;

import com.lhy.vo.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author：罗昊宇
 * @Package：com.lhy.controller
 * @Project：shiro-framwork
 * @name：LoginController
 * @Date：2024/5/9 0009  19:16
 * @Filename：LoginController
 */
@RestController
public class LoginController {

    @PostMapping("/login")
    public Result login(String username, String password){
        System.out.println(username);
        System.out.println(password);
        //获取当前用户的 Subject 对象，用于执行认证操作
        Subject subject = SecurityUtils.getSubject();
        //将用户输入的用户名和密码封装进去
        UsernamePasswordToken token =new UsernamePasswordToken(username,password);
        try {
            //进行用户身份认证
            subject.login(token);
//            return "index";
            return new Result(200,"登录成功",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(400,"账号或密码错误",null);
        }
    }
    @GetMapping("/unlogin")
    public Result unlogin(){
        return new Result(402,"未登录，请先登录",null);
    }
}
